﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Core;

namespace DataAccess.Classes
{
  public  class BaiViet
    { public int MaBaiViet { get; set; }
        public string TenBaiViet { get; set; }
        public string LoaiTin { get; set; }
        public string NoiDung { get; set; }
        public DateTime ThoiGian { get; set; }
        public string Hinh { get; set; }
        public string IDNguoiDung { get; set; }

        public BaiViet() { }

        public static List<BaiViet> LayDanhSach(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiViet_LayDanhSach", page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);

                reader.NextResult();
                return CBO.FillCollection<BaiViet>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiViet>();
            }
        }
        public static List<BaiViet> TimTheoNguoiDungViet(string word, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiViet_TimTheoNguoiDung", word, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);

                reader.NextResult();
                return CBO.FillCollection<BaiViet>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiViet>();
            }
        }
        public static List<BaiViet> BaiMoiViet()
        {
            try
            {
                return CBO.FillCollection<BaiViet>(DataProvider.Instance.ExecuteReader("BaiViet_BaiMoi"));
            }
            catch (Exception)
            {

                return null;
            }
        }

        public static BaiViet LayBaiVietTheoID(string id)
        {
            return CBO.FillObject<BaiViet>(DataProvider.Instance.ExecuteReader("BaiViet_LayTheoID", id));
        }

        #region Hien thi danh sach
        public static List<BaiViet> LayDanhSachTheoIDDiaDiem(string id, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiViet_LayTheoIDDiaDiem", Convert.ToInt32(id),
                                                            GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                            pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiViet>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0;
                return new List<BaiViet>();
            }
        }
        public static BaiViet Single(string id)
        {
            try
            {
                return CBO.FillObject<BaiViet>(DataProvider.Instance.ExecuteReader("BaiViet_Single", id));
            }
            catch { return null; }
        }

        public static List<BaiViet> TimKiem(string timkiemString, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiViet_TimKiem", timkiemString, GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiViet>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiViet>();
            }
        }
        public static List<BaiViet> LayTheoTinhTrang(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiViet_LayTheoTinhTrang", GlobalConfiguration.DescriptionLength,
                                                            page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiViet>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiViet>();
            }
        }
        #endregion


        #region Them
        public static bool Them(BaiViet baiviet)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("MaBaiViet", "BaiViet_Them", baiviet.MaBaiViet, baiviet.TenBaiViet, baiviet.LoaiTin,
                   baiviet.NoiDung, baiviet.ThoiGian, baiviet.Hinh, baiviet.IDNguoiDung);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {

                return false;
            }
        }
        #endregion


        public static int Dem(string idnguoidung)
        {

            try
            {
                object rs = DataProvider.Instance.ExecuteScalar("DemSoBaiDang", idnguoidung);
                return Convert.ToInt32(rs);
            }
            catch
            {
                return Convert.ToInt32(0);
            }
        }
        public static List<BaiViet> Paging(string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiViet_Paging", page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);

                reader.NextResult();
                return CBO.FillCollection<BaiViet>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiViet>();
            }
        }
       //public static bool CapNhat(BaiViet baiviet)
       // {
       //     try
       //     {
       //         //object rs = DataProvider.Instance.ExecuteNonQuery("BaiViet_CapNhat", baiviet.MaBaiViet, baiviet.ID, baiviet.TenBaiViet, baiviet.LoaiTin,
       //         //    baiviet.ThoiGian, baiviet.NoiDung, baiviet.HinhChinh, baiviet.HinhDaiDien, baiviet.TinhTrang);
       //         //return Convert.ToInt32(rs) > 0;
       //     }
       //     catch
       //     {

       //         return false;
       //     }
       // }

    }
}

